package com.flight.trans.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.flight.trans.R;
import com.flight.trans.activity.EnterPortPickingDetailsActivity;
import com.flight.trans.adapter.EnterPortPickingAdapter;
import com.flight.trans.adapter.EnterPortTakeAdapter;
import com.flight.trans.base.BaseFragment;
import com.flight.trans.bean.EnterPortTakeBean.EnterPortTakeData;
import com.flight.trans.global.Constant;
import com.flight.trans.global.ConstantInfo;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.listener.OnItemClickListener;
import com.flight.trans.util.ChinaToPinyin;
import com.flight.trans.util.ProgressUtils;
import com.flight.trans.view.HeaderTitleView;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lzy.okgo.model.HttpParams;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 进港出库
 */
public class EnterPortPickingFragment extends BaseFragment {

    private HeaderTitleView mHeaderView ;

    private TextView mTvTakeGoodsNo ,//提货单号
            mTvOrderCount ,//运单数量
            mTvTotalCount ,//总件数
            mTvTakePersonnel ,//提货人
            mTvReceivingPerson ,//收货人
            mTvDrawerPersonnel ,//开单人
            mTvPickingPersonnel ,//分拣人
            mTvPickingTime ;//分拣时间

    private TwinklingRefreshLayout mRefreshLayout ;
    private RecyclerView mRvContainer ;
    private RelativeLayout mRlContentNull ;

    private EnterPortPickingAdapter mPickingAdapter;
    private List<EnterPortTakeData> mTakeDataList ,mSearchDataList;

    private boolean IsChecked = true;
    private int mSortState = -1;

    @Override
    public int setContentView() {
        return R.layout.fragment_enter_port_picking;
    }

    @Override
    public void initView() {
        mTvTakeGoodsNo = mView.findViewById(R.id.mTvTakeGoodsNo);
        mTvOrderCount = mView.findViewById(R.id.mTvOrderCount);
        mTvTotalCount = mView.findViewById(R.id.mTvTotalCount);
        mTvTakePersonnel = mView.findViewById(R.id.mTvTakePersonnel);
        mTvDrawerPersonnel = mView.findViewById(R.id.mTvDrawerPersonnel);
        mTvPickingPersonnel = mView.findViewById(R.id.mTvPickingPersonnel);
        mTvReceivingPerson = mView.findViewById(R.id.mTvReceivingPerson);
        mTvPickingTime = mView.findViewById(R.id.mTvPickingTime);

        mHeaderView = mView.findViewById(R.id.mHeaderView);
        mRefreshLayout = mView.findViewById(R.id.mRefreshLayout);
        mRvContainer = mView.findViewById(R.id.mRvContainer);
        mRlContentNull = mView.findViewById(R.id.mRlContentNull);
    }

    @Override
    public void initData() {
        initHeaderView();

        mTvTakeGoodsNo.setOnClickListener(this::onClickSort);
        mTvOrderCount.setOnClickListener(this::onClickSort);
        mTvTotalCount.setOnClickListener(this::onClickSort);
        mTvTakePersonnel.setOnClickListener(this::onClickSort);
        mTvReceivingPerson.setOnClickListener(this::onClickSort);
        mTvDrawerPersonnel.setOnClickListener(this::onClickSort);
        mTvPickingPersonnel.setOnClickListener(this::onClickSort);
        mTvPickingTime.setOnClickListener(this::onClickSort);

        setRefreshListener(mRefreshLayout);
    }

    @Override
    public void initConnect() {
        if(mHeaderView == null) return;

        valueParams = new HttpParams();
        valueParams.put(Constant.Key_Is_Done , mHeaderView.isDone() ? 3 : 2);
        if(!TextUtils.isEmpty(mHeaderView.getSearchContent()))
            valueParams.put(Constant.Consignee_No , mHeaderView.getSearchContent());
        if(mHeaderView.isDone())
            valueParams.put(Constant.Key_Consignee_Date , mHeaderView.getSelectDate());

        NetworkUtils.getEnterPortPickingList(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                mTakeDataList = (List<EnterPortTakeData>) body;

                mSearchDataList = mTakeDataList ;

                initAdapter();
            }
        }, valueParams);
    }

    private void initAdapter(){
        mRlContentNull.setVisibility(mSearchDataList == null || mSearchDataList.size() == 0 ? View.VISIBLE : View.GONE);
        mRefreshLayout.setVisibility(mSearchDataList == null || mSearchDataList.size() == 0 ? View.GONE : View.VISIBLE);

        if(mSearchDataList != null)
            Collections.sort(mSearchDataList, new SortComparator(mSortState));

        if(mPickingAdapter == null) {
            mPickingAdapter = new EnterPortPickingAdapter(mActivity, mSearchDataList , IsChecked);
            mRvContainer.setAdapter(mPickingAdapter);
            mRvContainer.setLayoutManager(new LinearLayoutManager(mActivity));

            mPickingAdapter.setOnItemClickListener(new TakeItemClickListener());
        }else {
            mPickingAdapter.updateAdapter(mSearchDataList , IsChecked);
        }
    }

    /**
     * 条目点击事件
     */
    private class TakeItemClickListener extends OnItemClickListener {
        @Override
        public void onClick(View view, int position) {
            NetworkUtils.checkPageLock(new OkGoBackListener(mActivity) {
                @Override
                public void onSuccess(Object body) {
                    mIntent = new Intent(mActivity , EnterPortPickingDetailsActivity.class);
                    mBundle = new Bundle();
                    mBundle.putSerializable(Constant.INFO , mSearchDataList.get(position));
                    mBundle.putBoolean(Constant.IsEditor , !mHeaderView.isDone());
                    mIntent.putExtras(mBundle);
                    turnActForResult(mIntent);
                }
            }, ConstantInfo.IN_PICKING , mSearchDataList.get(position).getConsigneeNo());
        }
    }

    @Override
    public void onScanResult(String code) {
        mHeaderView.setSearchContent(code);

        if(!TextUtils.isEmpty(code)) {
            mSearchDataList = new ArrayList<>();

            for (int i = 0; i < mTakeDataList.size(); i++)
                if(mTakeDataList.get(i).getConsigneeNo() != null && mTakeDataList.get(i).getConsigneeNo().contains(code))
                    mSearchDataList.add(mTakeDataList.get(i));
        }else {
            mSearchDataList = mTakeDataList ;
        }

        initAdapter();
    }

    private void initHeaderView() {
        mHeaderView.addOnHeaderListener(new HeaderTitleView.OnHeaderListener(){
            @Override
            public void onSearch(String search) {
                initConnect();
            }

            @Override
            public void onPartChange(int isChecked) {
                IsChecked = isChecked == 1;

                mTvPickingPersonnel.setVisibility(IsChecked ? View.GONE : View.VISIBLE);
                mTvPickingTime.setVisibility(IsChecked ? View.GONE : View.VISIBLE);

                ProgressUtils.showProgress(mActivity);
                initConnect();
            }

            @Override
            public void onDateChange(String date) {
                ProgressUtils.showProgress(mActivity);
                initConnect();
            }
        });
    }

    public void onClickSort(View view){
        mSortState = (mSortState != 0 && R.id.mTvTakeGoodsNo == view.getId()) ? 0 : (mSortState == 0 ? -1 : mSortState) ;
        mSortState = (mSortState != 1 && R.id.mTvOrderCount == view.getId()) ? 1 : mSortState == 1 ? -1 : mSortState ;
        mSortState = (mSortState != 2 && R.id.mTvTotalCount == view.getId()) ? 2 : mSortState == 2 ? -1 : mSortState ;
        mSortState = (mSortState != 3 && R.id.mTvReceivingPerson == view.getId()) ? 3 : mSortState == 3 ? -1 : mSortState ;
        mSortState = (mSortState != 4 && R.id.mTvDrawerPersonnel == view.getId()) ? 4 : mSortState == 4 ? -1 : mSortState ;
        mSortState = (mSortState != 5 && R.id.mTvPickingPersonnel == view.getId()) ? 5 : mSortState == 5 ? -1 : mSortState ;

        mTvTakeGoodsNo.setTextColor(mActivity.getColor(mSortState == 0 ? R.color.color_red : R.color.color_blue));
        mTvOrderCount.setTextColor(mActivity.getColor(mSortState == 1 ? R.color.color_red : R.color.color_blue));
        mTvTotalCount.setTextColor(mActivity.getColor(mSortState == 2 ? R.color.color_red : R.color.color_blue));
        mTvReceivingPerson.setTextColor(mActivity.getColor(mSortState == 3 ? R.color.color_red : R.color.color_blue));
        mTvDrawerPersonnel.setTextColor(mActivity.getColor(mSortState == 4 ? R.color.color_red : R.color.color_blue));
        mTvPickingPersonnel.setTextColor(mActivity.getColor(mSortState == 5 ? R.color.color_red : R.color.color_blue));

        initAdapter();
    }

    /**
     * 排序集合
     */
    private class SortComparator implements Comparator<EnterPortTakeData> {

        private int type;

        public SortComparator(int type){
            this.type = type;
        }

        @Override
        public int compare(EnterPortTakeData data1, EnterPortTakeData data2) {
            switch (type){
                case 1 :
                    return data2.getOutNum() - data1.getOutNum();
                case 2 :
                    return data2.getRealNumber() - data1.getRealNumber();
                case 3 :
                    return ChinaToPinyin.getInstance().sort(data1.getReceiverName() , data2.getReceiverName());
                case 4 :
                    return ChinaToPinyin.getInstance().sort(data1.getConsigneeCreateByName() , data2.getConsigneeCreateByName());
                case 5 :
                    return ChinaToPinyin.getInstance().sort(data1.getOutboundUser() , data2.getOutboundUser());
                default:
                    return 0 ;
            }
        }
    }
}
